package com.jihe.ehmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jihe.common.utils.PageUtils;
import com.jihe.common.utils.Query;
import com.jihe.ehmall.product.dao.CategoryDao;
import com.jihe.ehmall.product.entity.CategoryEntity;
import com.jihe.ehmall.product.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listTree() {
        QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("show_status" , 1);
        List<CategoryEntity> categoryEntities = this.baseMapper.selectList(queryWrapper);

        return categoryEntities.stream().filter(entity ->
                entity.getParentCid() == 0 ).map(menu ->
        {menu.setChildren(getChildrens(menu, categoryEntities));return menu;
        }).collect(Collectors.toList());
    }

    @Override
    public void removeCategoryByIds(List<Long> asList) {
        if(asList.size() > 0){
            UpdateWrapper updateWrapper = new UpdateWrapper<>();

            updateWrapper.set("show_status",0);
            updateWrapper.in("cat_id",asList.toArray());
            this.baseMapper.update(new CategoryEntity(),updateWrapper);

        }


    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> list = new ArrayList<>();
        List<Long> catelogIdPaths = getParentIdByCatelogId(catelogId, list);
        Collections.reverse(catelogIdPaths);
        return catelogIdPaths.toArray(new Long[catelogIdPaths.size()]);
    }

    private List<Long> getParentIdByCatelogId(Long catelogId , List<Long> list) {
        list.add(catelogId);
        CategoryEntity categoryEntity = this.baseMapper.selectById(catelogId);
        if(categoryEntity.getParentCid() != 0){
            getParentIdByCatelogId(categoryEntity.getParentCid() , list);
        }
        return list;
    }

    private List<CategoryEntity> getChildrens(CategoryEntity root, List<CategoryEntity> categoryEntities) {

        return categoryEntities.stream().filter(entity -> entity.getParentCid() == root.getCatId()).map(menu -> {
            menu.setChildren(getChildrens(menu, categoryEntities));
            return menu;
        }).collect(Collectors.toList());
    }


}